% function basically for converting the data format used in
% batchTrackVasodilationVesselFill_v2_2 into a format that stores the
% tracking from a single experiment together
%
% update from v1.0: set up to allow for multiple ROI's per field of view

function groupLoopedTrackingData_diving_v2_0
warning('off','all')

RE = '_Metadata_';
fn = dir(['*',RE,'*.mat']);

for n = 1:length(fn)
    %% load in data about the stims
    load(fn(n).name,'ballTrackingData','stimparams','preStimTime','postStimTime')
    
    ind = strfind(fn(n).name,RE);
    if isempty(ind)
        continue;
    end
    
    rt = fn(n).name(1:(ind-1));
    temp = regexp(fn(n).name(ind:end),'\d*','Match');
    rt = [rt,temp{1}];
    fn2 = dir([rt,'*.tif']);
    r = [];
    if false
        if length(fn2) < 2              % this patch is here if you accidentally included an underscore before the experiment number
            rt = fn(n).name(1:(ind-1));
            temp = regexp(fn(n).name(ind:end),'\d*','Match');
            rt = [rt,'_',temp{1}];
            fn2 = dir([rt,'*.tif']);
            if length(fn2) < 2
                return;
            end
        end
    end
    for n2 = 1:length(fn2)          %this SHOULD correctly order the movies
        cur = strrep(fn2(n2).name,'.tif','_divingTracking3.mat');               %note this is grabbing the updated tracking; older version which poorly handled intensity inhomogeneity was called divingTracking.mat
        if ~isfile(cur)
            cur = strrep(fn2(n2).name,'.tif','_divingTracking4.mat');
        end
        try
            load(cur)
        catch
            disp([fn2(n2).name,' is not available']);
            continue;
        end
        if ~isfield(results,'params') || isempty(results(1).params)
            clear results
            continue;
        end
        
        if isempty(r)
            r = results;
            r = rmfield(r,'radonEstimate');
            firstMovName = convertCharsToStrings(r(1).moviename);
        else
            for n3 = 1:length(results)
                r(n3).params = cat(3,r(n3).params,results(n3).params);
                r(n3).ci = cat(4,r(n3).ci,results(n3).ci);
                r(n3).minoraxisFit = cat(3,r(n3).minoraxisFit,results(n3).minoraxisFit);
                r(n3).roiPosition = [r(n3).roiPosition;results(n3).roiPosition];        % check these approaches for if you have multiple ROIs in one movie
                r(n3).moviename = [r(n3).moviename,convertCharsToStrings(results(n3).moviename)];
            end
        end       
    end
    if isempty(r)
        continue;
    end
    for n2 = 1:length(r)
        r(n2).preStimTime = preStimTime;
        r(n2).postStimTime = postStimTime;
        r(n2).stimparams = stimparams;
    end
    [stiminfo,uniqueID,~,stimID] = getStimInfo_v4_4(firstMovName,fn(n).name,r(1));
    for n2 = 1:length(r)
        %r(n2).stimID = uid;        %older version
        r(n2).stimID = stimID';
        r(n2).ballTracking = [];
    end
    for n2 = 1:length(ballTrackingData)
        numframes = size(r(1).params,1);
        cur = ballTrackingData{n2};
        cur = cur(1:(length(cur)-1));
        cur = diff(cur);
        t =linspace(1,numframes,numel(cur));
        cur = interp1(t,cur,1:numframes,'nearest');
        for n3 = 1:length(r)
            r(n3).ballTracking = [r(n3).ballTracking;cur];
        end
    end

    results = r;
    savename = [rt,'_groupedDivingResults.mat'];
    save(savename,'results');
    clear r results
end

warning('on','all')

        
        
    